package com.ht.demo.service.manager.async.factory;

import com.ht.demo.common.pojo.bo.monitor.LoginInfoBO;
import com.ht.demo.common.pojo.bo.monitor.SysLoggerBO;
import com.ht.demo.common.pojo.bo.system.SysUserBO;
import com.ht.demo.common.util.JsonUtil;
import com.ht.demo.common.util.ruoyi.StringUtils;
import com.ht.demo.dao.domain.monitor.LoginInfoDO;
import com.ht.demo.service.manager.monitor.LoginInfoManager;
import com.ht.demo.service.manager.monitor.SysLoggerManager;
import com.ht.demo.service.system.AppContext;
import com.ht.demo.service.util.ServletUtils;
import com.ht.demo.service.util.ip.AddressUtils;
import com.ht.demo.service.util.ip.IpUtils;
import eu.bitwalker.useragentutils.UserAgent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.TimerTask;

/**
 * @Author hdengxian
 * @Date 2023/5/30 16:41
 */
public class AsyncFactory {
    private static final Logger sysLogger = LoggerFactory.getLogger("sys-logger");
    /**
     * 操作日志记录
     * @param loggerBO
     * @return 任务task
     */
    public static TimerTask recordOperate(final SysLoggerBO loggerBO) {
        return new TimerTask() {
            @Override
            public void run() {
                loggerBO.setLocation(AddressUtils.getRealAddressByIP(loggerBO.getOperIp()));
                AppContext.getBean(SysLoggerManager.class).insertLog(loggerBO);
                // 远程查询操作地点
            }
        };
    }


    public static TimerTask recordLoginInfo(final SysUserBO sysUserBO, final String loginStatus, final String message
            , String token, final Object... args) {
        final UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
        final String ip = IpUtils.getIpAddr();
        return new TimerTask() {
            @Override
            public void run() {
                String address = AddressUtils.getRealAddressByIP(ip);
                StringBuilder s = new StringBuilder();
                s.append(ip);
                s.append(address);
                s.append(sysUserBO.getUserName());
                s.append(loginStatus);
                s.append(message);
                sysLogger.info(s.toString(), args);

                String optionSys = userAgent.getOperatingSystem().getName();
                String browser = userAgent.getBrowser().getName();

                LoginInfoBO bo = new LoginInfoBO();
                bo.setLoginNo(sysUserBO.getLoginNo());
                bo.setUserName(sysUserBO.getUserName());
                bo.setToken(token);

                bo.setIpaddr(ip);
                bo.setLoginLocation(address);
                bo.setBrowser(browser);
                bo.setOptionSys(optionSys);
                bo.setMsg(message);
                bo.setLoginStatus(loginStatus);

                bo.setCreateBy("system auto");
                bo.setLoginTime(new Date());
                AppContext.getBean(LoginInfoManager.class).insertData(bo);

                sysLogger.info("AsyncFactory.recordLoginInfo - 异步任务记录登录完成 - LoginInfoBO={}", JsonUtil.toJson(bo));

            }
        };
    }


}
